package com.zzc.controller;

import com.zzc.domain.Client;
import com.zzc.service.impl.ClientServiceImpl;
import com.zzc.util.UpLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
public class ClientController {

    @Autowired
    ClientServiceImpl clientService;

    @Autowired
    UpLoad upLoad;

    @GetMapping("/getclient")
    public String getClient(Model model) {

        List<Client> allClient = clientService.getAllClient();
        model.addAttribute("clients", allClient);

        return "client/allClient";
    }

    @GetMapping("/addclient")
    public String addClient(Model model) {
        model.addAttribute("clientId", clientService.getClientCount() + 202000);
        return "client/addClient";
    }

    @PostMapping("/uploadclient")
    @ResponseBody
    public String upLoad(String name, String birth,
                         int clientId, String address,
                         int gender, String phoneNum,
                         String room, String family,
                         String familyPhone, String medicalHistory,
                         MultipartFile file) throws IOException {
        String uploadPath = upLoad.upload(file);

        Client client = new Client(clientId, name, phoneNum, address, room, gender, family, familyPhone, birth, uploadPath, medicalHistory);

        clientService.addClient(client);

        return "ok";
    }

    @GetMapping("/editClient/{clientid}")
    public String getEditClient(@PathVariable("clientid") int clientId,Model model) {
        model.addAttribute("client",clientService.findOneById(clientId));
        return "client/editClient";
    }

    @PostMapping("/updateclient")
    @ResponseBody
    public String upDateClient(String name, String birth,
                               int clientId, String address,
                               int gender, String phoneNum,
                               String room, String family,
                               String familyPhone, String medicalHistory,
                               MultipartFile file) throws IOException {

        if (file.isEmpty()){
            Client client = clientService.findOneById(clientId);
            String uploadPath = client.getClientPicture();
            Client client1 = new Client(clientId, name, phoneNum, address, room, gender, family, familyPhone, birth, uploadPath, medicalHistory);
            clientService.editClient(client1);
        }else {
            String uploadPath = upLoad.upload(file);

            Client client = new Client(clientId, name, phoneNum, address, room, gender, family, familyPhone, birth, uploadPath, medicalHistory);

            clientService.editClient(client);
        }

        return null;
    }

    @GetMapping("/getattributes/{clientId}")
    public String getAttributes(@PathVariable("clientId") int clientId,Model model){
        model.addAttribute("attribute",clientService.getClientAttributes(clientId));
        return "info";
    }
}
